package com.le.tester.book.concurrentjavafight.l8_3_3;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.math3.stat.descriptive.summary.Sum;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.Semaphore;
import java.util.stream.Collectors;

/**
 * createTime：2022/11/29 16:53
 * description：使用信号量来控制任务的提交速率
 */
public class BounderExecutor {
    private final Executor exec;

    private final Semaphore semaphore;


    public BounderExecutor(Executor exec, int bound) {
        this.exec = exec;
        this.semaphore = new Semaphore(bound);
    }

    public void submitTask(final Runnable command) throws InterruptedException {
        semaphore.acquire();
        try {
            exec.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        command.run();
                    } finally {
                        semaphore.release();
                    }
                }
            });
        } catch (Exception e) {
            semaphore.release();
        }
    }

    public static Map<String, String> mapStringToMap(String str) {
        String[] strS = str.split(";");
        Map<String, String> map = new HashMap<>();
        for (String string : strS) {
            String key = string.split("=")[0];
            String value = string.split("=")[1];
            // 去掉头部空格
            String key1 = key.trim();
            String value1 = value.trim();
            map.put(key1, value1);
        }
        return map;
    }

    public static void main(String[] args) {
        List<Long> temp = new ArrayList<>();
        temp.add(1L);
        temp.add(2L);
        temp.add(3L);
        for (Long item : temp) {
            System.out.println(item);
            if (item.equals(1L)) {
                return;
            }
        }
//        Optional<Long> reduce = temp.stream().reduce(Long::sum);
//        reduce.


    }


}
